import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login/index.vue'
import Main from '../views/Main'
import User from '../views/User'
import Home from '../views/Home'
import Search from "../views/Search"
import SearchResult from "@/views/SearchResult"
import ArticleDetail from "@/views/ArticleDetail"
import UserEdit from '@/views/UserEdit/UserEdit.vue'
import Chat from "@/views/Chat"
import store from '@/store/index.js'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Main,
    children: [
      {
        path: '/',
        component: Home
      },
      {
        path: '/user',
        component: User,
        meta:{isRecord:true,top:0}
      },
    ]
  },
  {
    path:"/search",
    component:Search
  },
  { path: '/search/result', component: SearchResult, name: "searchResult" },
  // 文章详细路由规则
  { path: '/article/:id', component: ArticleDetail,  name: 'art-detail',props: true},
  { path: "/UserEdit", component: UserEdit,name: 'user-edit'},
  { path: "/User/chat",component: Chat,name:"user-chat" }
]
const router = new VueRouter({
  routes
})
// 1. 将 VueRouter 本身提供的 $router.push 方法转存到常量中
const originalPush = VueRouter.prototype.push
// 2. 自定义 $router.push 方法，在内部调用原生的 originalPush 方法进行路由跳转；并通过 .catch 捕获错误
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  // 通过 .catch 捕获错误
  return originalPush.call(this, location).catch(err => err)
}
// 所有有权限页面的路径，都在这个数组之中
const pagePathArr = ['/user', '/user/edit']
// 为路由实例对象增加前置守卫

// 为路由的实例对象挂载全局前置守卫
router.beforeEach((to, from, next) => {
  // 访问的是有权限的页面，需要判断用户是否登录
  if (pagePathArr.indexOf(to.path) !== -1) {
    // 1. 从 store 中获取 token 的值
    //    注意：store.state.tokenInfo 要么是 {} 空对象，要么是包含 {token, refresh_token} 的对象
    const tokenStr = store.state.tokenInfo.token
    if (tokenStr) {
      // 1.1 token 有值，已登录过（操作：直接放行）
      next()
    } else {
      // 1.2 token 不存在，没有登录（操作：强制跳转到登录页）
      next(`/login?pre=${to.fullPath}`)
    }
  } else {
    // 访问的是没有权限的页面
    next()
  }
})



export default router




